package dao;

import Utils.DBUtil;
import bean.Problem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-01-22
 * Time: 17:33
 */
//针对Problem的增删改查
public class ProblemDao {
    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;

    //1.新增题目
    public void insert(Problem problem) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into oj_table values(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, problem.getTitle());
            statement.setString(2,problem.getLevel());
            statement.setString(3,problem.getDescription());
            statement.setString(4,problem.getTemplateCode());
            statement.setString(5,problem.getTestCode());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret != 1) {
                System.out.println("[ProblemDao] 新增题目失败!!");
            } else {
                System.out.println("[ProblemDao] 新增题目成功!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //2.删除题目
    public void delete(int id) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql语句
            String sql = "delete from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret != 1) {
                System.out.println("[ProblemDao] 删除题目失败!!");
            } else {
                System.out.println("[ProblemDao] 删除题目成功!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //3.查询题目列表 -- 把数据库所有的题目都给返回了
    //看需求,如果题库中的数据非常多的话就前后端都实现分页查询即可
    public List<Problem> selectAll() {
        List<Problem> problems = new ArrayList<>();
        try{
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql -- 没必要把所有的列都找出来,找题目序号,题目名称,难度就差不多了
            String sql = "select id,title,level from oj_table";
            statement = connection.prepareStatement(sql);
            //3.执行sql
            resultSet = statement.executeQuery();
            //4.遍历resultSet
            while(resultSet.next()) {
                Problem problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problems.add(problem);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return problems;
    }

    //4.查询题目详情
    public Problem selectOne(int id) {
        Problem problem = null;
        try {
            //1,和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼接sql语句
            String sql = "Select * from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            //3.执行sql语句
            resultSet = statement.executeQuery();
            //遍历查询结果 (就一个)
            if(resultSet.next()) {
                problem = new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                problem.setTestCode(resultSet.getString("testCode"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return problem;
    }





}
